package com.gpj.learn.springcloud.ribbon.loadbalanced.service;

import com.gpj.learn.springcloud.common.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @className RibbonService
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/9/27 17:07
 * @version 1.0
 **/
@Service
public class RibbonService {
	@Autowired
	private RestTemplate restTemplate;

	public String callInfo(Integer id){
		String url = String.format("http://%s/server/info/", Constants.COMMON_SERVER_MAIN_NAME);
		return restTemplate.getForObject(url + id, String.class);
	}

	public void callReceive(String message){
		String url = String.format("http://%s/server/receive", Constants.COMMON_SERVER_MAIN_NAME);
		MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
		paramMap.add("message", message);
		restTemplate.postForObject(url, paramMap, String.class);
	}
}
